/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.own.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.jeesite.modules.own.entity.OwnMedicineAssignment;
import com.jeesite.modules.own.service.OwnMedicineAssignmentService;
import com.jeesite.modules.sys.utils.UserUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.own.entity.OwnMedicineSubcontracting;
import com.jeesite.modules.own.service.OwnMedicineSubcontractingService;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;

/**
 * 药材分包信息Controller
 * @author F
 * @version 2019-10-09
 */
@Controller
@RequestMapping(value = "${adminPath}/own/ownMedicineSubcontracting")
public class OwnMedicineSubcontractingController extends BaseController {

	@Autowired
	private OwnMedicineSubcontractingService ownMedicineSubcontractingService;

	@Autowired
	private OwnMedicineAssignmentService ownMedicineAssignmentService;
	
	/**
	 * 获取数据
	 */
	@ModelAttribute
	public OwnMedicineSubcontracting get(String id, boolean isNewRecord) {
		return ownMedicineSubcontractingService.get(id, isNewRecord);
	}
	
	/**
	 * 查询列表
	 */
	@RequiresPermissions("own:ownMedicineSubcontracting:view")
	@RequestMapping(value = {"list", ""})
	public String list(OwnMedicineSubcontracting ownMedicineSubcontracting, Model model) {
		model.addAttribute("ownMedicineSubcontracting", ownMedicineSubcontracting);
		return "modules/own/ownMedicineSubcontractingList";
	}
	
	/**
	 * 查询列表数据
	 */
	@RequiresPermissions("own:ownMedicineSubcontracting:view")
	@RequestMapping(value = "listData")
	@ResponseBody
	public Page<OwnMedicineSubcontracting> listData(OwnMedicineSubcontracting ownMedicineSubcontracting, HttpServletRequest request, HttpServletResponse response) {
		ownMedicineSubcontracting.setPage(new Page<>(request, response));
		Page<OwnMedicineSubcontracting> page = ownMedicineSubcontractingService.findPage(ownMedicineSubcontracting);
		return page;
	}

	/**
	 * 查看编辑表单
	 */
	@RequiresPermissions("own:ownMedicineSubcontracting:view")
	@RequestMapping(value = "form")
	public String form(OwnMedicineSubcontracting ownMedicineSubcontracting, Model model) {
		model.addAttribute("ownMedicineSubcontracting", ownMedicineSubcontracting);
		return "modules/own/ownMedicineSubcontractingForm";
	}

	/**
	 * 保存药材分包信息
	 */
	@RequiresPermissions("own:ownMedicineSubcontracting:edit")
	@PostMapping(value = "save")
	@ResponseBody
	public String save(@Validated OwnMedicineSubcontracting ownMedicineSubcontracting) {
		ownMedicineSubcontractingService.save(ownMedicineSubcontracting);
		for (int i = 0; i < ownMedicineSubcontracting.getThisSubcontractingNum(); i ++){
			OwnMedicineAssignment ownMedicineAssignment = new OwnMedicineAssignment();
			ownMedicineAssignment.setOwnMedicineReceiptInformationId(ownMedicineSubcontracting.getOwnMedicineReceiptInformationId());
			ownMedicineAssignment.setOwnMedicineSubcontractingId(ownMedicineSubcontracting.getId());
			ownMedicineAssignment.setMedicineBatchId(ownMedicineSubcontracting.getMedicineBatchId());
			ownMedicineAssignment.setVarietyName(ownMedicineSubcontracting.getVarietyName());
			ownMedicineAssignment.setSourceCode(ownMedicineSubcontracting.getSourceCode());
			ownMedicineAssignment.setAreaCode(ownMedicineSubcontracting.getAreaCode());
			ownMedicineAssignment.setSpec(ownMedicineSubcontracting.getSpec());
			ownMedicineAssignment.setWeight(ownMedicineSubcontracting.getWeight());
			ownMedicineAssignment.setTime(new Date());
			String bmTrackCode = UUID.randomUUID().toString();
			ownMedicineAssignment.setBmTraceCode(bmTrackCode);
			ownMedicineAssignment.setSerialNumber(ownMedicineSubcontracting.getMedicineBatchId()+"_"+String.valueOf(i+1));
			ownMedicineAssignment.setBmBatchCode(ownMedicineSubcontracting.getBmBatchCode());
			ownMedicineAssignment.setCreateCompany(ownMedicineSubcontracting.getCreateCompany());
			ownMedicineAssignmentService.save(ownMedicineAssignment);
		}
		return renderResult(Global.TRUE, text("保存药材分包信息成功！"));
	}
	
	/**
	 * 删除药材分包信息
	 */
	@RequiresPermissions("own:ownMedicineSubcontracting:edit")
	@RequestMapping(value = "delete")
	@ResponseBody
	public String delete(OwnMedicineSubcontracting ownMedicineSubcontracting) {
		ownMedicineSubcontractingService.delete(ownMedicineSubcontracting);
		return renderResult(Global.TRUE, text("删除药材分包信息成功！"));
	}
	
}